package vn.com.mybudget.da.gae;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import vn.com.mybudget.da.inf.ITransactionDA;
import vn.com.mybudget.entities.Account;
import vn.com.mybudget.entities.Entry;
import vn.com.mybudget.entities.Entries;

public class TransactionDA implements ITransactionDA {

	@Override
	public Entry GetObjectByKey(Object key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account AddObject(Entry obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean DeleteObject(Entry obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Entry> ListByDate(Date viewDate, Account _currentAccount) {
		List<Entry> objectListByValue = null;
		try {
			objectListByValue = (List<Entry>) PMF.getObjectListByTwoValues(
					Entry.class, "createdDate", viewDate, "account",
					_currentAccount.getId());
			return objectListByValue;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return objectListByValue;

	}

	@Override
	public Entry AddTransaction(Entry trans) {
		return (Entry) PMF.save(trans);
	}

	@Override
	public boolean AddTransactionToUser(Account currentUser, Entry trans) {

		Calendar cl = Calendar.getInstance();
		Entry entry = new Entry();
		entry.setAmount(trans.getAmount());
		entry.setDescription(trans.getDescription());
		entry.setCreatedDate(cl.getTime());
		entry.setAccount(currentUser.getId());
		trans = this.AddTransaction(entry);
		if (trans == null) {
			return false;
		}
		return true;
	}

	@Override
	public List<Entry> ListByDateRange(Date from, Date to) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long GetCurrentBalance(Account _currentAccount) {
		List<Entry> balance = (List<Entry>) PMF.GetBalanceByAccount(
				Entry.class, _currentAccount.getId());
		long balanceSum = 0;
		for (int i = 0; i < balance.size(); i++) {
			balanceSum += balance.get(i).getAmount();
		}
		return balanceSum;
	}

	@Override
	public List<Entry> ListByMonth(Date dateStr, Account _currentAccount) {
		List<Entry> objectListByValue = null;
		try {
			objectListByValue = (List<Entry>) PMF.ListByMonth(Entry.class,
					"createdDate", dateStr, "account", _currentAccount.getId());
			return objectListByValue;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return objectListByValue;
	}

	@Override
	public boolean DeleteTransactionOfAccountByKey(String key, Key accountId) {
		Entry deleteEntry = (Entry) PMF.getObjectById(Entry.class, Long.parseLong(key));
		deleteEntry.setDescription("111111111111111111111111");
		PMF.save(deleteEntry);
		if (deleteEntry.getAccount().getId() == accountId.getId() && deleteEntry.getAccount().getClass() == accountId.getClass()) {
			try {
				return PMF.deleteEntryByKey(Long.parseLong(key));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			return false;
		}
		return false;
	}

	@Override
	public boolean UpdateEntry(Account _currentAccount, Entry transaction) {
		Entry updateEntry = (Entry) PMF.getObjectById(Entry.class, transaction.getId());
		updateEntry.setDescription(transaction.getDescription());
		updateEntry.setAmount(transaction.getAmount());
		updateEntry = (Entry) PMF.save(updateEntry);
		return updateEntry != null;
	}

	/**/
}
